import java.util.*;

/**
 * @author LKQ
 * @date 2022/4/17 10:13
 * @description 采用迭代的方式，实时维护
 */
public class Solution2 {
    public static void main(String[] args) {

    }
    class BSTIterator {
        private TreeNode cur;
        private Deque<TreeNode> stack;

        public BSTIterator(TreeNode root) {
            cur = root;
            stack = new LinkedList<TreeNode>();
        }

        public int next() {
            while (cur != null) {
                stack.push(cur);
                cur = cur.left;
            }
            // 弹出的最左边的节点
            cur = stack.pop();
            int ret = cur.val;
            cur = cur.right;
            return ret;
        }

        public boolean hasNext() {
            return cur != null || !stack.isEmpty();
        }
    }
}
